import {doGetRequest, doJsonRequest, doPostRequest, doSubmitForm, JsonResp} from "../common/request.ts";
import {
  CommentInfo, CommentPageParam, CommentParam,
  HotTag,
  PageInfo, Question,
  QuestionInfo,
  QuestionPage,
  QuestionQuery, TagInfo
} from "../types/commonTypes.ts";
const apiUrl = import.meta.env.VITE_API_URL
export type LoginType = {
  redirectUrl: string,
  type: string
}

export function getLoginTypes(): Promise<JsonResp<LoginType[]>> {
  return doJsonRequest(`${apiUrl}/login/types`)
}

type LoginParams = {
  loginCode: string,
  loginType: string,
}

export function doLogin(params: LoginParams): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/login/callback`, params)
}

export function doLogout(): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/login/out`)
}

export function doGetQuestions(param: QuestionQuery): Promise<JsonResp<QuestionPage>> {
  return doPostRequest(`${apiUrl}/questions/index`, param)
}

export function doGetHotTags(): Promise<JsonResp<{ topList: HotTag[], allList: TagInfo[] }>> {
  return doGetRequest(`${apiUrl}/tag/all`)
}

export function doGetQuestionDetail(id: string): Promise<JsonResp<QuestionInfo>> {
  return doGetRequest(`${apiUrl}/questions/${id}`)
}

export function doGetCommentPage(qid: string, pageParam: CommentPageParam): Promise<JsonResp<PageInfo<CommentInfo>>> {
  return doPostRequest(`${apiUrl}/questions/${qid}/comments`, pageParam)
}

export function doPraiseRequest(qid: string, status: number): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/questions/${qid}/praise`, {status: status})
}

export function doPraiseComment(id: string, status: number): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/questions/comments/${id}/praise`, {status: status})
}

export function doDeleteQuestion(qid: string): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/questions/${qid}/delete`)
}

export function doSubmitComment(qid: string, param: CommentParam): Promise<JsonResp<string>> {
  return doPostRequest(`${apiUrl}/questions/${qid}/comment`, param)
}

export function doDeleteComment(cid: string): Promise<JsonResp<string>> {
  return doGetRequest(`${apiUrl}/questions/comment/${cid}/delete`)
}

export function doGetQuestionInfo(qid: string): Promise<JsonResp<Question>> {
  return doGetRequest(`${apiUrl}/questions/${qid}/detail`)
}

export function doUploadImg(img: File): Promise<JsonResp<string>> {
  const formData = new FormData();
  formData.append('file', img);
  return doSubmitForm(`${apiUrl}/file/img-upload`, formData)
}

export function doSaveOrUpdateQuestion(question: Question): Promise<JsonResp<string>> {
  return doPostRequest(`${apiUrl}/questions/publish/save-or-update`, question)
}
export function doUpdateQuestionContent(question: Question): Promise<JsonResp<string>> {
  return doPostRequest(`${apiUrl}/questions/publish/content-update`, question)
}